package cn.sciento.farm.devicemanager.service.impl;

import cn.sciento.farm.devicemanager.common.util.BaseContextHandler;
import cn.sciento.farm.devicemanager.dao.entity.TenantConfig;
import cn.sciento.farm.devicemanager.dao.mapper.TenantConfigMapper;
import cn.sciento.farm.devicemanager.service.TenantConfigService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.databind.ser.Serializers;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class TenantConfigServiceImpl implements TenantConfigService {

    @Autowired
    private TenantConfigMapper tenantConfigMapper;

    @Override
    public int update(TenantConfig tenantConfig) {
        TenantConfig config = get(null);
        if (config == null){
            tenantConfig.setCreateTime(System.currentTimeMillis());
            tenantConfig.setUpdateTime(System.currentTimeMillis());
            return tenantConfigMapper.insert(tenantConfig);
        }else {
            tenantConfig.setUpdateTime(System.currentTimeMillis());
            tenantConfig.setId(config.getId());
            return tenantConfigMapper.updateById(tenantConfig);
        }
    }


    @Override
    public TenantConfig get(String tenantId) {
        String te = BaseContextHandler.getTenantId();
        QueryWrapper queryWrapper = new QueryWrapper<TenantConfig>();
        if(StringUtils.isEmpty(te)){
            queryWrapper.eq("tenant_id",tenantId);
        }
        TenantConfig tenantConfig = tenantConfigMapper.selectOne(queryWrapper);
        return tenantConfig;
    }

    @Override
    public int delete(String tenantId) {
        return 0;
    }
}
